3 use Wikimedia\ScopedCallback
;
4 use Wikimedia\TestingAccessWrapper
;
7 * @author Matthias Mullie <mmullie@wikimedia.org>
11 class BagOStuffTest
extends MediaWikiTestCase
{
15 const TEST_KEY
= 'test';
17 protected function setUp() {
20 // type defined through parameter
21 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
22 $name = $this->getCliArg( 'use-bagostuff' );
24 $this->cache
= ObjectCache
::newFromId( $name );
26 // no type defined - use simple hash
27 $this->cache
= new HashBagOStuff
;
30 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
31 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
35 * @covers MediumSpecificBagOStuff::makeGlobalKey
36 * @covers MediumSpecificBagOStuff::makeKeyInternal
38 public function testMakeKey() {
39 $cache = ObjectCache
::newFromId( 'hash' );
41 $localKey = $cache->makeKey( 'first', 'second', 'third' );
42 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
44 $this->assertStringMatchesFormat(
45 '%Sfirst%Ssecond%Sthird%S',
47 'Local key interpolates parameters'
50 $this->assertStringMatchesFormat(
51 'global%Sfirst%Ssecond%Sthird%S',
53 'Global key interpolates parameters and contains global prefix'
56 $this->assertNotEquals(
59 'Local key and global key with same parameters should not be equal'
62 $this->assertNotEquals(
63 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
69 * @covers MediumSpecificBagOStuff::merge
70 * @covers MediumSpecificBagOStuff::mergeViaCas
72 public function testMerge() {
73 $key = $this->cache
->makeKey( self
::TEST_KEY
);
76 $casRace = false; // emulate a race
77 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
81 $cache->set( $key, 'conflict', 5 );
84 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
87 // merge on non-existing value
88 $merged = $this->cache
->merge( $key, $callback, 5 );
89 $this->assertTrue( $merged );
90 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
92 // merge on existing value
93 $merged = $this->cache
->merge( $key, $callback, 5 );
94 $this->assertTrue( $merged );
95 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
100 $this->cache
->merge( $key, $callback, 5, 1 ),
101 'Non-blocking merge (CAS)'
104 if ( $this->cache
instanceof MultiWriteBagOStuff
) {
105 $wrapper = TestingAccessWrapper
::newFromObject( $this->cache
);
106 $this->assertEquals( count( $wrapper->caches
), $calls );
108 $this->assertEquals( 1, $calls );
113 * @covers MediumSpecificBagOStuff::changeTTL
115 public function testChangeTTL() {
117 $this->cache
->setMockTime( $now );
119 $key = $this->cache
->makeKey( self
::TEST_KEY
);
122 $this->cache
->add( $key, $value, 5 );
123 $this->assertEquals( $value, $this->cache
->get( $key ) );
124 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
125 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
126 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
127 $this->assertEquals( $this->cache
->get( $key ), $value );
128 $this->cache
->delete( $key );
129 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
131 $this->cache
->add( $key, $value, 5 );
132 $this->assertTrue( $this->cache
->changeTTL( $key, $now - 3600 ) );
133 $this->assertFalse( $this->cache
->get( $key ) );
137 * @covers MediumSpecificBagOStuff::changeTTLMulti
139 public function testChangeTTLMulti() {
141 $this->cache
->setMockTime( $now );
143 $key1 = $this->cache
->makeKey( 'test-key1' );
144 $key2 = $this->cache
->makeKey( 'test-key2' );
145 $key3 = $this->cache
->makeKey( 'test-key3' );
146 $key4 = $this->cache
->makeKey( 'test-key4' );
149 $this->cache
->delete( $key1 );
150 $this->cache
->delete( $key2 );
151 $this->cache
->delete( $key3 );
152 $this->cache
->delete( $key4 );
154 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
155 $this->assertFalse( $ok, "No keys found" );
156 $this->assertFalse( $this->cache
->get( $key1 ) );
157 $this->assertFalse( $this->cache
->get( $key2 ) );
158 $this->assertFalse( $this->cache
->get( $key3 ) );
160 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
162 $this->assertTrue( $ok, "setMulti() succeeded" );
165 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
166 "setMulti() succeeded via getMulti() check"
169 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
170 $this->assertTrue( $ok, "TTL bumped for all keys" );
171 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
172 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
173 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
175 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], $now +
86400 );
176 $this->assertTrue( $ok, "Expiry set for all keys" );
178 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
179 $this->assertFalse( $ok, "One key missing" );
181 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
182 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
183 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
186 $this->cache
->delete( $key1 );
187 $this->cache
->delete( $key2 );
188 $this->cache
->delete( $key3 );
189 $this->cache
->delete( $key4 );
193 * @covers MediumSpecificBagOStuff::add
195 public function testAdd() {
196 $key = $this->cache
->makeKey( self
::TEST_KEY
);
197 $this->assertFalse( $this->cache
->get( $key ) );
198 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
199 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
203 * @covers MediumSpecificBagOStuff::get
205 public function testGet() {
206 $value = [ 'this' => 'is', 'a' => 'test' ];
208 $key = $this->cache
->makeKey( self
::TEST_KEY
);
209 $this->cache
->add( $key, $value, 5 );
210 $this->assertEquals( $this->cache
->get( $key ), $value );
214 * @covers MediumSpecificBagOStuff::get
215 * @covers MediumSpecificBagOStuff::set
216 * @covers MediumSpecificBagOStuff::getWithSetCallback
218 public function testGetWithSetCallback() {
220 $this->cache
->setMockTime( $now );
221 $key = $this->cache
->makeKey( self
::TEST_KEY
);
223 $this->assertFalse( $this->cache
->get( $key ), "No value" );
225 $value = $this->cache
->getWithSetCallback(
231 return 'hello kitty';
235 $this->assertEquals( 'hello kitty', $value );
236 $this->assertEquals( $value, $this->cache
->get( $key ), "Value set" );
240 $this->assertFalse( $this->cache
->get( $key ), "Value expired" );
244 * @covers MediumSpecificBagOStuff::incr
246 public function testIncr() {
247 $key = $this->cache
->makeKey( self
::TEST_KEY
);
248 $this->cache
->add( $key, 0, 5 );
249 $this->cache
->incr( $key );
251 $actualValue = $this->cache
->get( $key );
252 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
256 * @covers MediumSpecificBagOStuff::incrWithInit
258 public function testIncrWithInit() {
259 $key = $this->cache
->makeKey( self
::TEST_KEY
);
260 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
261 $this->assertEquals( 3, $val, "Correct init value" );
263 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
264 $this->assertEquals( 4, $val, "Correct init value" );
268 * @covers MediumSpecificBagOStuff::getMulti
270 public function testGetMulti() {
271 $value1 = [ 'this' => 'is', 'a' => 'test' ];
272 $value2 = [ 'this' => 'is', 'another' => 'test' ];
273 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
274 $value4 = [ 'another test where chars in key will be encoded' ];
276 $key1 = $this->cache
->makeKey( 'test-1' );
277 $key2 = $this->cache
->makeKey( 'test-2' );
278 // internally, MemcachedBagOStuffs will encode to will-%25-encode
279 $key3 = $this->cache
->makeKey( 'will-%-encode' );
280 $key4 = $this->cache
->makeKey(
281 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
285 $this->cache
->delete( $key1 );
286 $this->cache
->delete( $key2 );
287 $this->cache
->delete( $key3 );
288 $this->cache
->delete( $key4 );
290 $this->cache
->add( $key1, $value1, 5 );
291 $this->cache
->add( $key2, $value2, 5 );
292 $this->cache
->add( $key3, $value3, 5 );
293 $this->cache
->add( $key4, $value4, 5 );
296 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
297 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
301 $this->cache
->delete( $key1 );
302 $this->cache
->delete( $key2 );
303 $this->cache
->delete( $key3 );
304 $this->cache
->delete( $key4 );
308 * @covers MediumSpecificBagOStuff::setMulti
309 * @covers MediumSpecificBagOStuff::deleteMulti
311 public function testSetDeleteMulti() {
313 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
314 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
315 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
316 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
317 $this->cache
->makeKey( 'test-5' ) => 4,
318 $this->cache
->makeKey( 'test-6' ) => 'ever'
321 $this->assertTrue( $this->cache
->setMulti( $map ) );
324 $this->cache
->getMulti( array_keys( $map ) )
327 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
331 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
335 $this->cache
->getMulti( array_keys( $map ) )
340 * @covers MediumSpecificBagOStuff::get
341 * @covers MediumSpecificBagOStuff::getMulti
342 * @covers MediumSpecificBagOStuff::merge
343 * @covers MediumSpecificBagOStuff::delete
345 public function testSetSegmentable() {
346 $key = $this->cache
->makeKey( self
::TEST_KEY
);
348 $small = wfRandomString( 32 );
349 // 64 * 8 * 32768 = 16777216 bytes
350 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
352 $callback = function ( $cache, $key, $oldValue ) {
353 return $oldValue . '!';
356 foreach ( [ $tiny, $small, $big ] as $value ) {
357 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
358 $this->assertEquals( $value, $this->cache
->get( $key ) );
359 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key] );
361 $this->assertTrue( $this->cache
->merge( $key, $callback, 5 ) );
362 $this->assertEquals( "$value!", $this->cache
->get( $key ) );
363 $this->assertEquals( "$value!", $this->cache
->getMulti( [ $key ] )[$key] );
365 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ) );
366 $this->assertFalse( $this->cache
->get( $key ) );
367 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
369 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
370 $this->assertEquals( "@$value", $this->cache
->get( $key ) );
371 $this->assertTrue( $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
) );
372 $this->assertFalse( $this->cache
->get( $key ) );
373 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
376 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
378 $this->assertEquals( 666, $this->cache
->get( $key ) );
379 $this->assertEquals( 667, $this->cache
->incr( $key ) );
380 $this->assertEquals( 667, $this->cache
->get( $key ) );
382 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
383 $this->assertEquals( 664, $this->cache
->get( $key ) );
385 $this->assertTrue( $this->cache
->delete( $key ) );
386 $this->assertFalse( $this->cache
->get( $key ) );
390 * @covers MediumSpecificBagOStuff::getScopedLock
392 public function testGetScopedLock() {
393 $key = $this->cache
->makeKey( self
::TEST_KEY
);
394 $value1 = $this->cache
->getScopedLock( $key, 0 );
395 $value2 = $this->cache
->getScopedLock( $key, 0 );
397 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
398 $this->assertNull( $value2, 'Duplicate call returned no lock' );
402 $value3 = $this->cache
->getScopedLock( $key, 0 );
403 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
406 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
407 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
409 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
410 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
414 * @covers MediumSpecificBagOStuff::__construct
415 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
417 public function testReportDupes() {
418 $logger = $this->createMock( Psr\Log\NullLogger
::class );
419 $logger->expects( $this->once() )
420 ->method( 'warning' )
421 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
426 $cache = new HashBagOStuff( [
427 'reportDupes' => true,
428 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
431 $cache->get( 'foo' );
432 $cache->get( 'bar' );
433 $cache->get( 'foo' );
435 DeferredUpdates
::doUpdates();
439 * @covers MediumSpecificBagOStuff::lock()
440 * @covers MediumSpecificBagOStuff::unlock()
442 public function testLocking() {
444 $this->assertTrue( $this->cache
->lock( $key ) );
445 $this->assertFalse( $this->cache
->lock( $key ) );
446 $this->assertTrue( $this->cache
->unlock( $key ) );
449 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
450 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
451 $this->assertTrue( $this->cache
->unlock( $key2 ) );
452 $this->assertTrue( $this->cache
->unlock( $key2 ) );
455 public function tearDown() {
456 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
457 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );